//
//  NewfeatureViewController.m
//  新浪微博
//
//  Created by hbl on 16/2/19.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "NewfeatureViewController.h"
#import "BLTabBarViewController.h"
#import "Access.h"
#import "Oauth2ViewController.h"
#define ImageNumber 3
#define BLColor(r,g,b) [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1.0]
@interface NewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak)UIPageControl *pageControl;
@end

@implementation NewfeatureViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupScrollerView];
    [self setupPageController];
}

- (void)setImageButton:(UIImageView *)img
{
    //进入微博按钮
    img.userInteractionEnabled = YES;
    UIButton *btn = [[UIButton alloc]init];
    [btn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    btn.bounds = CGRectMake(0, 0, btn.currentBackgroundImage.size.width, btn.currentBackgroundImage.size.height);
    btn.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.6);
    [btn setTitle:@"开始微博" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [img addSubview:btn];
    [btn addTarget:self action:@selector(beginWb) forControlEvents:UIControlEventTouchUpInside];
    
    //添加checkBox
    UIButton *checkBox = [[UIButton alloc]init];
    [checkBox setTitle:@"分享给大家" forState:UIControlStateNormal];
    [checkBox setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [checkBox setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [checkBox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    checkBox.bounds = CGRectMake(0, 0, 150, 30);
    checkBox.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.52);
    [img addSubview:checkBox];
    [checkBox addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)beginWb
{
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [doc stringByAppendingPathComponent:@"access.data"];
    Access *access = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (access) {
        self.view.window.rootViewController = [[BLTabBarViewController alloc]init];
    }else{
        self.view.window.rootViewController = [[Oauth2ViewController alloc]init];
    }
    
}

- (void)check:(UIButton *)check
{
    check.selected = !check.selected;
}

- (void)setupPageController
{
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.bounds = CGRectMake(0, 0, 70, 30);
    pageControl.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height - 30);
    pageControl.numberOfPages = ImageNumber;
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.pageIndicatorTintColor = BLColor(133.0, 133.0, 133.0);
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

- (void)setupScrollerView
{
    UIScrollView *scroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:scroller];
    scroller.delegate = self;
    scroller.pagingEnabled = YES;
    scroller.showsHorizontalScrollIndicator = NO;
    scroller.bounces = NO;
    
    CGFloat imgW = self.view.frame.size.width;
    CGFloat imgH = self.view.frame.size.height;
    for (int i = 0; i<ImageNumber; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        NSString *imgName = [NSString stringWithFormat:@"new_feature_%d",i + 1];
        imgView.image = [UIImage imageNamed:imgName];
        
        CGFloat imgX = i * imgW;
        CGFloat imgY = 0;
        imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
        [scroller addSubview:imgView];
        if (i == ImageNumber - 1) {
            [self setImageButton:imgView];
        }
    }
    
    scroller.contentSize = CGSizeMake(imgW * ImageNumber, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat realPage = (scrollView.contentOffset.x /self.view.frame.size.width) + 0.5;
    int currentPage = (int) realPage;
    self.pageControl.currentPage = currentPage;
}
@end
